package com.backend.thread.countDownLatch

import java.util.concurrent.CountDownLatch

//Get latch object in constructor so that after completing the task, thread can countDown() the latch
abstract class BaseHealthChecker
(val serviceName: String, private val _latch: CountDownLatch?) : Runnable {
    var isServiceUp: Boolean = false
        private set

    init {
        this.isServiceUp = false
    }

    override fun run() {
        try {
            verifyService()
            isServiceUp = true
        } catch (t: Throwable) {
            t.printStackTrace(System.err)
            isServiceUp = false
        } finally {
            _latch?.countDown()
        }
    }

    //This methos needs to be implemented by all specific service checker
    abstract fun verifyService()
}